﻿using main.sdk.input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace main.src.game
{

    public class Game_Player : Game_Character
    {

        private static Texture2D[] Textures = {
                                          Program.Self.Content.Load<Texture2D>("rsc/player/Stop_1"),
                                          Program.Self.Content.Load<Texture2D>("rsc/player/Stop_2"),
                                          Program.Self.Content.Load<Texture2D>("rsc/player/Stop_3"),
                                          Program.Self.Content.Load<Texture2D>("rsc/player/Moving_1"),
                                          Program.Self.Content.Load<Texture2D>("rsc/player/Moving_2"),
                                          Program.Self.Content.Load<Texture2D>("rsc/player/Jumping")
                                      };

        public Game_Item item;

        private int TimeElapsed = 0;

        private const uint ANIM_TIME = 100;

        public Game_Player(float x, float y)
        {
            Texture = Textures[0];
            Bounds.Offset((int)x, (int)y);
            Update();
        }

        public override void Update()
        {
            Scale.X = Program.Width * 0.12f / Width;
            Scale.Y = Program.Height * 0.21f / Height;
            base.Update();
            if (IsFalling() || IsJumping())
                Texture = Textures[5];
            else if (IsMoving)
            {
                TimeElapsed += Program.Time.ElapsedGameTime.Milliseconds;
                if (TimeElapsed >= ANIM_TIME)
                {
                    Texture = Texture == Textures[3] ? Textures[4] : Textures[3];
                    TimeElapsed = 0;
                }
            }
            else
            {
                TimeElapsed += Program.Time.ElapsedGameTime.Milliseconds;
                if (TimeElapsed >= ANIM_TIME)
                {
                    Texture = Texture == Textures[0] ? Textures[1] : Texture == Textures[1] ? Textures[2] : Textures[0];
                    TimeElapsed = 0;
                }
            }
            if (!(IsFalling() || IsJumping()) && Input.Trigger(Keys.Space))
                Jump();
            if (Input.Press(Keys.Left))
                Move(6);
            if (Input.Press(Keys.Right))
                Move(4);
            if (Input.Trigger(Keys.A))
            {
                item = new Game_Item(0, Direction);
                item.Bounds.X = Bounds.Right;
                item.Bounds.Y = Bounds.Y + Bounds.Height / 2;
            }
            if (item != null)
                item.Update();
        }

        public void Die()
        {

        }

    }

}